﻿using CallCenter.Data.Base;
using CallCenter.Data.Models;
using CallCenter.Data.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Data.BizUtilities
{
    public class RoleUtility : BaseUtility
    {
        public RoleUtility(DataManager manager)
            : base(manager)
        { }

     
        public List<RoleModelVM> GetRoles()
        {
            string sql = "select * from tbl_RolesData";

            return this.Factory.ExecuteObjects<RoleModelVM>(sql);

        }

        //public bool InsertRole(RoleModelVM model)
        //{
        //    using (var cmd = this.Factory.GetCommand("usp_RoleCrud", true, false)
        //        .AddParameter("@pMode", 0)
        //        .AddParameter("@pRoleName", model.RoleName))
        //    {
        //        var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
        //        param.Direction = System.Data.ParameterDirection.Output;
        //        return true;
        //    }
        //}

        public ReturnMessage<RoleModelVM> InsertRole(RoleModelVM model)
        {
            var returnMessage = new ReturnMessage<RoleModelVM>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Insert(model.RoleName, model.Description);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.Id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }

        public ReturnMessage<RoleModelVM> UpdateRole(RoleModelVM model)
        {
            var returnMessage = new ReturnMessage<RoleModelVM>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Update(model.RoleName,model.Description,model.Id);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.Id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }
    
        public int Insert(string rolename, string description) //insert data to a contact table
        {
            int nRtn = -1;
            string sql = "INSERT INTO tbl_RolesData (rolename, description) Values(@rolename, @description) SELECT @pResult =@@IDENTITY";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@rolename", SqlDbType.NVarChar, 10, rolename);
                this.Factory.AddParameter(cmd, "@description", SqlDbType.NVarChar, 100, description);

                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;

        }
        public int Update(string rolename, string description, int id) //insert data to a title table
        {
            int nRtn = -1;
            string sql = "UPDATE tbl_RolesData SET rolename = @rolename, description = @desc WHERE id =@id";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@rolename", SqlDbType.NVarChar, 10, rolename);
                this.Factory.AddParameter(cmd, "@desc", SqlDbType.NVarChar, 100, description);

                this.Factory.AddParameter(cmd, "@id", SqlDbType.Int, 10, id);
                this.Factory.ExecuteNonQuery(cmd);

            }
            nRtn = id;
            return nRtn; //retruning the data stored in the table
        }


        public void Delete(int roleId) //delete the row of data using the id passed to the function
        {
            string sql = "DELETE FROM tbl_RolesData " +
                  "WHERE ID = @ID ";


            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 10, roleId);
                this.Factory.ExecuteNonQuery(cmd);
            }

        }


        public List<UserModelVm> GetRegisterUsers()
        {
            string sql = "select * from tbl_Users where isactive is null";

            return this.Factory.ExecuteObjects<UserModelVm>(sql);
        }

        public void UpdateRegisterUsers(RegisterViewModel user)
        {
            string password = "";
            password = CryptUtils.GetPasswordEncrypted(user.Password);
            string sql = "update tbl_Users set password = '" + password + "' where id = '" + user.Id + "'";
            this.Factory.ExecuteNonQuery(sql);
        }

      
    }
}
